package day06;

/**
 * 二叉树三种基本的遍历方法实现：中序，先序，后序，三者遍历方式相同，只是输出位置有所不同
 */
public class Answer {

    public static void main(String[] args) {
        loop(initTree());
    }

    public static NodeTree initTree() {
        NodeTree tree = new NodeTree(1);
        tree.left = new NodeTree(2);
        tree.right = new NodeTree(3);

        tree.left.left = new NodeTree(4);
        tree.left.right = new NodeTree(5);

        tree.right.left = new NodeTree(6);
        tree.right.right = new NodeTree(7);
        return tree;
    }

    public static void loop(NodeTree root) {
        if (root.left != null) {
            loop(root.left);
        }
        if (root.right != null) {
            loop(root.right);
        }
        System.out.println("value：" + root.val);
    }

    /**
     * 二叉树
     */
    static class NodeTree {
        int val;
        NodeTree left;
        NodeTree right;

        public NodeTree(int val) {
            this.val = val;
        }
    }
}
